<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-form-item label="常用项类别" prop="type" label-width="100px">
        <el-select v-model="queryParams.type" placeholder="请选择常用项类别" size="small" @change="getFrequentUsed">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
        >新增常用项</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="frequentUsedList">
      <el-table-column label="项目名" align="center" prop="name" />
      <el-table-column label="项目单价" align="center" prop="price" v-if="queryParams.type!=4"/>
      <el-table-column label="项目编码" align="center" prop="code" v-if="queryParams.type==4"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="queryTotal>0"-->
<!--      :total="queryTotal"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getInfo"-->
<!--    />-->

    <!--新增常用项-->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form">
        <el-form-item prop="name">
          <el-input v-model="form.name" placeholder="搜索项目名" @blur="getListByName" style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <el-table :data="addFrequentUsedList" @row-click="clickRow">
        <el-table-column label="项目名" align="center" prop="name" />
        <el-table-column label="项目单价" align="center" prop="price" />
        <el-table-column label="项目编码" align="center" prop="code" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="form.pageNum"
        :limit.sync="form.pageSize"
        @pagination="queryAllFrequent"
      />
    </el-dialog>
  </div>

</template>

<script>
import { getFrequent_used, getDrug, addFrequent_used, updateFrequent_used } from "@/api/sms/frequent_used";
import { listNon_drug } from "@/api/dms/non_drug";
import { listDise } from "@/api/dms/dise";
import { listDrug, getFrequentUsedDrug } from "@/api/dms/drug";

export default {
  name: "FrequentUsed",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      queryTotal:0,
      //常用项查询表格数据
      frequentUsedList:[],
      //新增常用项查询表格数据
      addFrequentUsedList:[],
      // 药品模板类型字典
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        type:'1',
        pageNum:1,
        pageSize:10,
      },
      // 表单参数
      form: {
        pageNum:1,
        pageSize:10,
      },
      //价格与项目编码的显示与隐藏
      showPrice:false,
      showCode:false,
      //成药，草药
      drug_id_list:[],
      //常用项信息
      frequentUsedInfo:null,
    };
  },
  created() {
    this.getFrequentUsed();
    this.getDicts("sms_frequent_used_list").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /**下拉框改变获取常用项项目*/
    getFrequentUsed(){
      this.queryParams.pageNum = 1;
      this.getInfo();
    },
    /** 查询所属人的常用项 */
    getInfo(){
      getFrequent_used(this.queryParams).then(response => {
        this.frequentUsedList = response.rows;
        this.queryTotal = response.total;
      })
    },

    /**新增常用项按钮事件 */
    handleAdd(){
      this.open=true;
      this.form.pageNum = 1;
      this.queryAllFrequent();
      this.resetForm("form");
    },
    /** 失焦事件  根据项目名称获取常用项数据*/
    getListByName(){
      this.form.pageNum = 1;
      this.queryAllFrequent();
    },
    //条件查询类别的常用项
    queryAllFrequent(){
      this.addFrequentUsedList = [];
      this.form.recordType = this.queryParams.type;
      if(this.queryParams.type == 1 || this.queryParams.type == 2 || this.queryParams.type == 3){
        listNon_drug(this.form).then(response => {
          this.addFrequentUsedList = response.rows;
          this.total = response.total;
        })
      }else if(this.queryParams.type == 4){
        listDise(this.form).then(response => {
          this.addFrequentUsedList = response.rows;
          this.total = response.total;
        })
      }else if(this.queryParams.type == 5 || this.queryParams.type == 6){
        listDrug(this.form).then(response => {
          this.addFrequentUsedList = response.rows;
          this.total = response.total;
        })
      }
    },
    /** 行点击事件 */
    clickRow(row){
      for (let i = 0;i < this.frequentUsedList.length;i++){
        if(this.frequentUsedList[i].id == row.id){
          this.$notify({title:'警告',message:'该常用项已存在！',type:'warning'});
          return;
        }
      }
      addFrequent_used(this.queryParams.type,row.id).then(response => {
        if(response.code == 200){
          this.msgSuccess("新增成功");
        }
        this.getFrequentUsed(this.queryParams.type);
      })
    },
    /**删除常用项项目*/
    handleDelete(row){
      updateFrequent_used(this.queryParams.type,row.id).then(response => {
        if(response.code == 200){
          this.msgSuccess("删除成功");
        }
        this.getFrequentUsed(this.queryParams.type);
      })
    },
  }
};
</script>
